有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java出现编译错误,我不理解

我是一个非常沮丧的新手! 我真的不知道从哪里开始。我有一个基于《傻瓜书》中的应用程序。让它工作起来。然后尝试将其转换为使用WIMMOne对话框表示时间和日期。在一系列错误的开始之后,我加载了他们的一个样本,并使其工作。然后,我隔离了其中的代码并粘贴到我的示例中。我不能克服错误。(eclipse不断地添加自己的一些内容并没有什么帮助。有时我加载源代码,所有语句(包括导入)都标记为错误,有时我获取R.id,---错误的话,我甚至将它的注释标记为错误

然而,我已经清理了它,并重新加载了足够多的时间,我认为我有一些真正的错误

我的代码和示例代码之间有几个基本区别: 这个示例是一个大型活动java文件,我的是一个片段。我的时间对话框有一个单独的java文件。样本在一个文件中,所以我用这种方式编码了我的。我的有两个菜单,一个列表,用户从中进入“细节”菜单。样品都在一个屏幕上

WIMM有两个JAR文件,我的LIB中都有

为了节省空间,我包括了第一部分,其中我有3个错误,但如果需要,我很乐意提供所有这些

首先是样本。它可以工作,即使使用我的注释代码,也没有编译错误: 谢谢,克拉克

    package com.test.medication;


    /**************************
     * Java Imports
     **************************/
    import java.util.Calendar;

    /******************************
     * 安卓 imports
     ******************************/
    import 安卓.app.AlarmManager;
    import 安卓.app.Dialog;
    import 安卓.app.PendingIntent;
    import 安卓.content.Context;
    import 安卓.content.Intent;
    import 安卓.net.Uri;
    import 安卓.os.Bundle;
    import 安卓.text.format.Time;
    import 安卓.util.Log;
    import 安卓.view.View;
    import 安卓.widget.Button;
    import 安卓.widget.EditText;

    /******************************
     * Wimm imports
     ******************************/
    import com.wimm.framework.app.LauncherActivity;
    import com.wimm.framework.app.TextInputDialog;
    import com.wimm.framework.app.TimePickerDialog;
    import com.wimm.framework.app.TimePickerDialog.OnTimeSelectedListener;

    public class MedicationsActivity extends LauncherActivity 
    {


        /****************************
         * private globals
         ****************************/
        private TextInputDialog textDialog;
        private TimePickerDialog timeDialog;
        private long msToCount = 0;
        private static String textInput = "please enter pills";
        private EditText timeDisplay;
        private EditText pillsDisplay;
        private long newMs = 0;
        private int Hours;
        private int Minutes;
        private static int hoursToCount = 0;
        private static int minutesToCount = 0;
        private Calendar c ;
        private int mIndex = 0;
        private boolean textEntered = false;

    // get an store the simple name of this class into an String
    private static final String TAG = MedicationsActivity.class.getSimpleName();


     /***********************
     *  define constants
     **********************/
    // define id for both the time dialog, and the enter pills dialog
    static final int TIME_DIALOG_ID = 0;
    static final int PILLS_DIALOG_ID = 1;
    // the actions for the intent definitions posted 
    public static final String ACTION_POST_NOTIFICATION     = "com.test.medication.action.POST";
    public static final String ACTION_UPDATE_NOTIFICATION   = "com.test.medication.action.UPDATE";
    public static final String ACTION_CANCEL_NOTIFICATION   = "com.test.medication.action.CANCEL";

    //delay between views
    private static final int viewDelay      = 5000;  // ms

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {

        //-----------------------------------------------
        // initialize time entry listener
            // pass current state
            super.onCreate(savedInstanceState);

            // open up main layout resource and upload into wimm 
            setContentView(R.layout.main);

            Log.d("MedsMC","S onCrt-1");

            // Subscribe listeners to the buttons and text fields
            Button okButton = (Button)findViewById(R.id.ok);
            Button pillsButton = (Button)findViewById(R.id.enterPills);
            pillsDisplay = (EditText)findViewById(R.id.entryMeds);
            timeDisplay = (EditText)findViewById(R.id.entryTime); 
            Log.d("MedsMC","S onCrt-2L" + timeDisplay);

            // set up the time picker dialog with initial time
            //and subscribe a listener upon the press of ok n the time dialog
            TimePickerDialog myTimerHandler = new TimePickerDialog(this);
            myTimerHandler.setTime(6, 0);
            myTimerHandler.setTimeSelectedListener(timeSetListener);
            Log.d("MedsMC","S onCrt-3myTH" + timeDisplay);

这是我的最新版本,其中有3个错误:“TimePickerDialog”用于多个地方,并且总是有错误

package com.dummies.安卓.taskreminder;
import java.text.SimpleDateFormat; 
import java.util.Calendar;  
import java.util.Date;

import 安卓.app.DatePickerDialog.OnDateSetListener;
//import 安卓.app.TimePickerDialog.OnTimeSetListener;
import 安卓.content.ContentUris;
import 安卓.content.ContentValues;
import 安卓.database.Cursor;
import 安卓.net.Uri;   //
import 安卓.os.Bundle;  //
import 安卓.os.Handler; 
import 安卓.support.v4.app.DialogFragment;
import 安卓.support.v4.app.Fragment;
import 安卓.support.v4.app.FragmentTransaction;
import 安卓.support.v4.app.LoaderManager.LoaderCallbacks;
import 安卓.support.v4.content.CursorLoader;
import 安卓.support.v4.content.Loader;
import 安卓.view.LayoutInflater;
import 安卓.view.View;  //
//import 安卓.view.View.OnClickListener;
import 安卓.view.ViewGroup;
import 安卓.widget.Button; //
import 安卓.widget.DatePicker;
import 安卓.widget.EditText;  //
//import 安卓.widget.TimePicker;
import 安卓.widget.Toast;
import 安卓.util.Log;  //
import 安卓.text.format.Time;
import 安卓.app.Dialog; //
import 安卓.content.Context;
import 安卓.content.Intent;

/*******************************************
 * wimm imports
 */

import com.wimm.framework.app.LauncherActivity;
//import com.wimm.framework.app.TextInputDialog;
mport com.wimm.framework.app.TimePickerDialog;
import com.wimm.framework.app.TimePickerDialog.OnTimeSelectedListener;
//import com.wimm.framework.app.DatePickerDialog;
//import com.wimm.framework.app.DatePickerDialog.OnDateSelectedListener;


public class ReminderEditFragment
             extends Fragment
             implements OnDateSetListener,
           //             OnTimeSetListener,
                        LoaderCallbacks<Cursor> 
   {

    private TimePickerDialog timeDialog;
    //private EditText timeDisplay;

    private static int intHours = 0;
    private static int intMinutes = 0;
    private long lngNewMs = 0;
    private Calendar calC ;

    // define constent to be used 
    private static final String DATE_FORMAT = "yyyy-MM-dd";
    private static final String TIME_FORMAT = "kk:mm";

    static final int TIME_DIALOG_ID = 0;
    static final int PILLS_DIALOG_ID = 1;

    public static final String DEFAULT_EDIT_FRAGMENT_TAG = "editFragmentTag"; 
    private EditText mTitleText;

    private EditText mBodyText;
    //private TextInputDialog textDialog;
    //private TextInputDialog mBodyDialog;

    //private TimePickerDialog timeDialog;
    private Button mTimeButton;


    //private Button timeDisplay;

    //private TimePickerDialog mTimeDialog;

    private Button mDateButton;
    private Button mConfirmButton;
    private Calendar mCalendar;

    //OK
    @Override
    public void onSaveInstanceState(Bundle outState)
       {
        super.onSaveInstanceState(outState);
        // save the calendar instance
            outState.putSerializable(CALENDAR, mCalendar);
       }

    private long mRowId;        


    //OK
    @Override
    public void onCreate(Bundle savedInstanceState)
       { 
        //-- house keeping
        super.onCreate(savedInstanceState);
        Log.d("RemindEF","S onCrt");

        if (savedInstanceState != null &&
            savedInstanceState.containsKey(CALENDAR) )
           {
             mCalendar = (Calendar) savedInstanceState.getSerializable(CALENDAR);
             Log.d("RemindEF","Cs onCrt:" + mCalendar);
           }
        else
           {
             mCalendar = Calendar.getInstance();
             Log.d("RemindEF","C onCrt:" + mCalendar);
           }
        Bundle arguments = getArguments(); 

        //-----------------
        //  retrieve row id from list
        if (arguments != null)
           {
            mRowId = arguments.getLong(ReminderProvider.COLUMN_ROWID);
            Log.d("RemindEF","A onCrt:" + mRowId);
               }                

        //-----------------------------------------------
        // initialize time entry listener
    //???????? ERROR: The method setContentView(int) is undefined for the type ReminderEditFragment
           setContentView(R.layout.reminder_edit);

           Log.d("RemindEF","X onCrt");

    //???????ERROR: The method findViewById(int) is undefined for the type ReminderEditFragment
           mTimeButton = (Button)findViewById(R.id.reminder_time); 
           Log.d("MedsMC","S onCrt-2L" + mTimeButton);

    //?????? ERROR: The constructor TimePickerDialog(ReminderEditFragment) is undefined
            TimePickerDialog myTimerHandler = new TimePickerDialog(this);
            myTimerHandler.setTime(6, 0);
            myTimerHandler.setTimeSelectedListener(timeSetListener);
            Log.d("MedsMC","S onCrt-3myTH" + mTimeButton);

共 (3) 个答案

  1. # 1 楼答案

    这主要是试图解释编译错误的含义


    ERROR: The method setContentView(int) is undefined for the type ReminderEditFragment

    您正在对ReminderEditFragment调用一个setContentView方法,但是ReminderEditFragment没有这样的方法

    似乎您正试图在this上调用Activity.setContentView。但是this不是一个Activity。。。因为你的重构

    ERROR: The method findViewById(int) is undefined for the type ReminderEditFragment

    同上,用于不同的Activity方法

    (我不是安卓专家,但你似乎在一个片段上做你应该在一个活动上做的事情。也许,你的片段类需要一个对某个活动实例的引用,它可以用于这些调用…?)


    ERROR: The constructor TimePickerDialog(ReminderEditFragment) is undefined

    标准的TimePickerDialog类有2个构造函数,但它们都不接受单个ReminderEditFragment参数。您需要查看TimePickerDialogjavadoc

  2. # 2 楼答案

    1)当激活XML文件时,不要运行应用程序
    若这样做了,请清理项目并删除生成的文件

    2)在向布局添加小部件后,始终保存项目Ctrl + Shift + S。保存后,将生成此小部件的地址,并且不会出现任何问题
    The method findViewById(int) is undefined for the type ReminderEditFragment

  3. # 3 楼答案

    您正在复制在Activity中使用的代码,并在Fragment中使用它

    这需要一些修改

    setContentView(R.layout.reminder_edit);
    

    这用于设置布局。它在onCreate()内调用

    mTimeButton = (Button) findViewById(R.id.reminder_time); 
    

    这用于获取对某个视图的引用

    Fragment中,要设置布局并查找视图,我们将覆盖onCreateView()

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.reminder_edit, null);
        mTimeButton = (Button) v.findViewById(R.id.reminder_time); 
        return v;
    }
    


    TimePickerDialog myTimerHandler = new TimePickerDialog(this);
    

    TimePickerDialog构造函数的参数是Context对象。 在活动中this将是有效的,因为它将引用活动的实例。 在片段中,您应该调用getActivity()

    TimePickerDialog myTimerHandler = new TimePickerDialog(getActivity());